package test;

import com.alibaba.fastjson.JSON;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import way.learning.zz.source.FileElement;
import way.learning.zz.source.FtpFile;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.Callable;

/**
 * <pre>
 * Created with IntelliJ IDEA.
 * User: zhengzhi
 * Date: 2017/9/13
 * Time: 9:10
 * To change this template use File | Settings | File Templates.
 * </pre>
 *
 * @author Administrator
 */
public class AnotherFtpMonitor {

    private String host;
    private int port;
    private String username;
    private String password;
    private static Set<String> fileList = new HashSet<String>();
    private String dir;


    //构造函数
    public AnotherFtpMonitor(String host, int port, String username, String password,String workingDirectory){
        this.host = host;
        this.port = port;
        this.username = username;
        this.password = password;
        this.dir = workingDirectory;
    }




    public void start(){
        FileGetTimerTask task = new FileGetTimerTask();

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task,0L,3*1000);

    }




    public static void main(String[] args) {

        String host = "119.23.14.212";
        int port = 21001;
        String username = "ndycfd2";
        String password = "1Q3qKcr";
        String workingDirectory = "/";

        AnotherFtpMonitor anotherFtpMonitor = new AnotherFtpMonitor(host, port, username, password, workingDirectory);
        anotherFtpMonitor.start();


        System.out.println(JSON.toJSONString(fileList));


    }


   class FileGetTimerTask extends TimerTask{

       private FTPClient ftpClient = null;


       public boolean login(){
           try {

               System.out.println("ftp连接信息：" + host + "," +  port + ",");

               //构造ftpClient，连接到远程ftp服务器
               ftpClient = new FTPClient();
               ftpClient.connect(host, port);
               ftpClient.enterLocalPassiveMode();

               if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                   ftpClient.disconnect();
                   throw new IOException("Exception when connecting to FTP Server: " + ftpClient);
               }

               //登录远程ftp服务器
               return ftpClient.login(username, password);
           } catch (Exception e) {
               e.printStackTrace();
           } finally {
               try {
                   if (ftpClient.isConnected()) {
                       ftpClient.disconnect();
                   }
               } catch (Throwable ioe) {
                   // do nothing
               }
           }

           return false;
       }

        public Set<String> listFiles() throws IOException {
            Set<String> result = new LinkedHashSet<String>();
            for (FTPFile file : ftpClient.listFiles(dir)) {
                result.add(file.getName());
            }
            return result;
        }

        @Override
        public void run() {
            if(login()){
                System.out.println("连接服务器成功");
            }
            fileList.clear();
            try {
                System.out.println("保存当前目录下的文件");
                Set<String> files = listFiles();
                fileList.addAll(files);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}
